/*
* jlb
*/
package com.jlb.miner.ctrl;

import com.alibaba.fastjson.JSON;
import com.jlb.base.ctrl.AppBaseCtrl;
import com.jlb.core.entity.BeanRet;
import com.jlb.core.entity.Page;
import com.jlb.core.tools.StringTools;
import com.jlb.core.tools.redis.RedisUtils;
import com.jlb.miner.entity.MinerItem;
import com.jlb.miner.entity.MinerItemState;
import com.jlb.miner.service.MinerItemSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 矿机 控制器
 *
 * @author jlb
 */
@RestController
@RequestMapping("/app/minerItem")
@Slf4j
@Api(value = "矿机控制器", description = "用户端：矿机控制器")
public class AppMinerItemCtrl extends AppBaseCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private MinerItemSV minerItemSV;

    /**
     * 查询MinerItem一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询MinerItem一个详情信息", notes = "查询MinerItem一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "Authorization", value = "授权信息", paramType = "query"),
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(@ApiIgnore HttpServletRequest request, Long id, String code) {
        if (id == null && code == null) {
            return null;
        }

        MinerItem minerItem = minerItemSV.load(id, code);
        log.info(JSON.toJSONString(minerItem));
        return BeanRet.create(true, "查询成功", minerItem);
    }


    /**
     * 根据条件code查询MinerItem一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询MinerItem一个详情信息", notes = "查询MinerItem一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path"),
            @ApiImplicitParam(name = "Authorization", value = "授权信息", paramType = "query"),
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        MinerItem minerItem = minerItemSV.loadByCode(code);
        log.info(JSON.toJSONString(minerItem));
        return BeanRet.create(true, "查询成功", minerItem);
    }

    /**
     * 购买矿机
     *
     * @return BeanRet
     */
    @ApiOperation(value = "购买矿机", notes = "购买矿机")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "minerCode", value = "矿机编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "payType", value = "支付类型", paramType = "query", required = true),
            @ApiImplicitParam(name = "pinPassword", value = "交易密码", paramType = "query", required = true),
            @ApiImplicitParam(name = "Authorization", value = "授权信息", paramType = "query"),
    })
    @PostMapping("/purchase")
    @ResponseBody
    public BeanRet purchase(@ApiIgnore HttpServletRequest request, String minerCode, String payType, String pinPassword) {
        if (StringTools.isEmpty(minerCode) || StringTools.isEmpty(payType) || StringTools.isEmpty(pinPassword)) {
            return BeanRet.create(false, "参数为空");
        }
        String userCode = this.getUserCode(request);
        minerItemSV.purchase(minerCode, userCode, payType, pinPassword);
        return BeanRet.create(true, "购买成功");
    }


    /**
     * 升级矿机
     *
     * @return BeanRet
     */
    @ApiOperation(value = "升级矿机", notes = "升级矿机")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "minerCode", value = "矿机编码", paramType = "query"),
            @ApiImplicitParam(name = "payType", value = "支付类型", paramType = "query", required = true),
            @ApiImplicitParam(name = "pinPassword", value = "交易密码", paramType = "query", required = true),
            @ApiImplicitParam(name = "Authorization", value = "授权信息", paramType = "query"),
    })
    @PostMapping("/upgrade")
    @ResponseBody
    public BeanRet xupgrade(@ApiIgnore HttpServletRequest request, String minerCode, String payType, String pinPassword) {
        if (StringTools.isEmpty(minerCode) || StringTools.isEmpty(payType) || StringTools.isEmpty(pinPassword)) {
            return BeanRet.create(false, "参数为空");
        }
        String userCode = this.getUserCode(request);
        minerItemSV.upgrade(minerCode, userCode, payType, pinPassword);

        return BeanRet.create(true, "成功升级");
    }


    /**
     * 复投矿机
     *
     * @return BeanRet
     */
    @ApiOperation(value = "复投矿机", notes = "复投矿机")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "payType", value = "支付类型", paramType = "query", required = true),
            @ApiImplicitParam(name = "pinPassword", value = "交易密码", paramType = "query", required = true),
            @ApiImplicitParam(name = "Authorization", value = "授权信息", paramType = "query"),
    })
    @PostMapping("/recast")
    @ResponseBody
    public BeanRet recast(@ApiIgnore HttpServletRequest request, String payType, String pinPassword) {
        if (StringTools.isEmpty(payType) || StringTools.isEmpty(pinPassword)) {
            return BeanRet.create(false, "参数为空");
        }
        String userCode = this.getUserCode(request);
        minerItemSV.recast(userCode, payType, pinPassword);

        return BeanRet.create(true, "复投成功");
    }


    /**
     * 用户查询到的矿机
     *
     * @return 分页对象
     */
    @ApiOperation(value = "用户查询到的矿机", notes = "用户查询到的矿机")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "Authorization", value = "授权信息", paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore MinerItem minerItem, Integer curPage, Integer pageSize) {
        Page<MinerItem> page = new Page<MinerItem>(pageSize, curPage);
        minerItem.setState(MinerItemState.Sales.name());
        List<MinerItem> minerItems = minerItemSV.list(minerItem, page.genRowStart(), page.getPageSize());
        page.setVoList(minerItems);
        int total = minerItemSV.count(minerItem);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);
    }


}
